package com.ycsj.goldmedalnewconcept.common.util;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.bean.UnitStateInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于请求某个单元对应的预习和复习状态的通用工具类
 * Created by Administrator on 2019/4/28 0028.
 */

public class UnitStateUtil {

    /**
     * 请求对应单元的预习和复习状态的方法
     *
     * @param context  上线文
     * @param unit     单元号,注意传入的格式:需要的是4位格式1001,1018,2001,2018。。。。4001,4018等
     * @param classnum 班级号,学生端因为登录时保存了班级号,调用时传""即可
     * @param listener 回调接口
     */
    public static void http(Context context, String unit, String classnum, final onUnitStateListener listener) {

        String acc = AccountUtils.getAccount(context);
        String role = AccountUtils.getRole(context);
        String apptoken = AccountUtils.getToken(context);

        Map<String, String> map = new HashMap<>();
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("unit", unit);

        if ("2".equals(role)) {
            //老师端
            map.put("type", "15");
            String schoolid = AccountUtils.getSchoolId(context);
            map.put("schoolid", schoolid);
            map.put("classnum", classnum);

        } else if ("0".equals(role)) {
            //学生端
            map.put("type", "17");
            String ttel = AccountUtils.getTeacher(context);
            String classsnum = AccountUtils.getClassNum(context);
            map.put("ttel", ttel);
            map.put("classnum", classsnum);
        }

        LogUtils.iTag("test", "url是:" + HttpAddress.UNITSTATE + "?type=17&ttel=" +
                AccountUtils.getTeacher(context) + "&classnum=" + classnum + "&apptoken=" + apptoken
                + "&unit=" + unit + "&acc=" + acc + "&role=" + role + "&platform=2"
        );

        HttpUtils.post(HttpAddress.UNITSTATE, map, context, new HttpCallBack<UnitStateInfo>() {
            @Override
            public void onSuccess(UnitStateInfo unitStateInfo) {
                if (null != listener) {
                    listener.onSuccess(unitStateInfo);
                }
            }

            @Override
            public void onError(String e) {
                if (null != listener) {
                    listener.onError();
                }
            }
        });
    }

    //回调接口
    public interface onUnitStateListener {
        void onSuccess(UnitStateInfo unitStateInfo);

        void onError();
    }

    /**
     * 请求服务器修改错题状态
     *
     * @param moduleid 1表示词汇检测 2预习检测 4语法检测
     */
    public static void httpState(Context context, String unit, String classNum, String moduleid, final onSycStateListener listener) {
        String acc = AccountUtils.getAccount(context);
        String role = AccountUtils.getRole(context);
        String apptoken = AccountUtils.getToken(context);

        Map<String, String> map = new HashMap<>();
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("moduleid", moduleid);

        if ("2".equals(role)) {
            //老师端
            map.put("type", "16");
            String schoolid = AccountUtils.getSchoolId(context);
            map.put("schoolid", schoolid);
            map.put("unit", unit);
            map.put("classnum", classNum);

        } else if ("0".equals(role)) {
            //学生端
            map.put("type", "18");
            String ttel = AccountUtils.getTeacher(context);
            String classsnum = AccountUtils.getClassNum(context);
            map.put("ttel", ttel);
            map.put("classnum", classsnum);
            map.put("unit", unit);
        }

        HttpUtils.post(HttpAddress.SYSERRORSTATE, map, context, new HttpCallBack<ResultInfo>() {
            @Override
            public void onSuccess(ResultInfo resultInfo) {
                if ("200".equals(resultInfo.getState())) {
                    if (listener != null) {
                        listener.onSuccess();
                    }
                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + resultInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                LogUtils.iTag("test", "同步错误状态失败");
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });

    }

    //修改状态的回调接口
    public interface onSycStateListener {
        void onSuccess();
    }
}
